{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "* 日期：2022-10-19 \\ week09\n",
    "* 作业：尝试列表所有方法\n",
    "* 记录人：陈蔚"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 1.len(list)\n",
    "列表元素个数"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "list1 = ['Google', 'Runoob', 'Taobao']\n",
    "print (len(list1))\n",
    "list2=list(range(5)) # 创建一个 0-4 的列表\n",
    "print (len(list2))"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 2.max(list)\n",
    "返回列表元素最大值"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "list1 最大元素值 :  Taobao\n",
      "list2 最大元素值 :  700\n"
     ]
    }
   ],
   "source": [
    "list1, list2 = ['Google', 'Runoob', 'Taobao'], [456, 700, 200]\n",
    "\n",
    "print (\"list1 最大元素值 : \", max(list1))\n",
    "print (\"list2 最大元素值 : \", max(list2))"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 3.min(list)\n",
    "返回列表元素最小值"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "list1 最小元素值 :  Google\n",
      "list2 最小元素值 :  200\n"
     ]
    }
   ],
   "source": [
    "list1, list2 = ['Google', 'Runoob', 'Taobao'], [456, 700, 200]\n",
    "\n",
    "print (\"list1 最小元素值 : \", min(list1))\n",
    "print (\"list2 最小元素值 : \", min(list2))"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 4.list(seq)\n",
    "将元组转换为列表"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列表元素 :  [123, 'Google', 'Runoob', 'Taobao']\n",
      "列表元素 :  ['H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd']\n"
     ]
    }
   ],
   "source": [
    "aTuple = (123, 'Google', 'Runoob', 'Taobao')\n",
    "list1 = list(aTuple)\n",
    "print (\"列表元素 : \", list1)\n",
    "\n",
    "str=\"Hello World\"\n",
    "list2=list(str)\n",
    "print (\"列表元素 : \", list2)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 5.list.append(obj)\n",
    "在列表末尾添加新的对象"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "更新后的列表 :  ['Google', 'Runoob', 'Taobao', 'Baidu']\n"
     ]
    }
   ],
   "source": [
    "list1 = ['Google', 'Runoob', 'Taobao']\n",
    "list1.append('Baidu')\n",
    "print (\"更新后的列表 : \", list1)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 6.list.count(obj)\n",
    "统计某个元素在列表中出现的次数"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "123 元素个数 :  2\n",
      "Runoob 元素个数 :  1\n"
     ]
    }
   ],
   "source": [
    "aList = [123, 'Google', 'Runoob', 'Taobao', 123];\n",
    "\n",
    "print (\"123 元素个数 : \", aList.count(123))\n",
    "print (\"Runoob 元素个数 : \", aList.count('Runoob'))"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 7.list.extend(seq)\n",
    " 在列表末尾一次性追加另一个序列中的多个值（用新列表扩展原来的列表）"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "扩展后的列表： ['Google', 'Runoob', 'Taobao', 0, 1, 2, 3, 4]\n"
     ]
    }
   ],
   "source": [
    "list1 = ['Google', 'Runoob', 'Taobao']\n",
    "list2=list(range(5)) # 创建 0-4 的列表\n",
    "list1.extend(list2)  # 扩展列表\n",
    "print (\"扩展后的列表：\", list1)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "新列表:  ['French', 'English', 'German', 'Spanish', 'Portuguese']\n",
      "新列表:  ['French', 'English', 'German', 'Spanish', 'Portuguese', 'Japanese', 'Chinese']\n"
     ]
    }
   ],
   "source": [
    "\n",
    "# 语言列表\n",
    "language = ['French', 'English', 'German']\n",
    "\n",
    "# 元组\n",
    "language_tuple = ('Spanish', 'Portuguese')\n",
    "\n",
    "# 集合\n",
    "language_set = {'Chinese', 'Japanese'}\n",
    "\n",
    "# 添加元组元素到列表末尾\n",
    "language.extend(language_tuple)\n",
    "\n",
    "print('新列表: ', language)\n",
    "\n",
    "# 添加集合元素到列表末尾\n",
    "language.extend(language_set)\n",
    "\n",
    "print('新列表: ', language)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "\n",
    "## 8.list.index(obj)\n",
    "从列表中找出某个值第一个匹配项的索引位置"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Runoob 索引值为 1\n",
      "Taobao 索引值为 2\n"
     ]
    }
   ],
   "source": [
    "list1 = ['Google', 'Runoob', 'Taobao']\n",
    "print ('Runoob 索引值为', list1.index('Runoob'))\n",
    "print ('Taobao 索引值为', list1.index('Taobao'))"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Runoob 索引值为 1\n"
     ]
    }
   ],
   "source": [
    "list1 = ['Google', 'Runoob', 'Taobao', 'Facebook', 'QQ']\n",
    "# 从指定位置开始搜索\n",
    "print ('Runoob 索引值为', list1.index('Runoob',1))"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 9.list.insert(index, obj)\n",
    "将对象插入列表"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列表插入元素后为 :  ['Google', 'Baidu', 'Runoob', 'Taobao']\n"
     ]
    }
   ],
   "source": [
    "list1 = ['Google', 'Runoob', 'Taobao']\n",
    "list1.insert(1, 'Baidu')\n",
    "print ('列表插入元素后为 : ', list1)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 10.list.pop([index=-1])\n",
    "移除列表中的一个元素（默认最后一个元素），并且返回该元素的值"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列表现在为 :  ['Google', 'Runoob']\n",
      "列表现在为 :  ['Google']\n"
     ]
    }
   ],
   "source": [
    "list1 = ['Google', 'Runoob', 'Taobao']\n",
    "list1.pop()\n",
    "print(\"列表现在为 : \", list1)\n",
    "list1.pop(1)\n",
    "print(\"列表现在为 : \", list1)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 11.list.remove(obj)\n",
    "移除列表中某个值的第一个匹配项"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列表现在为 :  ['Google', 'Runoob', 'Baidu']\n",
      "列表现在为 :  ['Google', 'Runoob']\n"
     ]
    }
   ],
   "source": [
    "list1 = ['Google', 'Runoob', 'Taobao', 'Baidu']\n",
    "list1.remove('Taobao')\n",
    "print (\"列表现在为 : \", list1)\n",
    "list1.remove('Baidu')\n",
    "print (\"列表现在为 : \", list1)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 12.list.reverse()\n",
    "反向列表中元素"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列表反转后:  ['Baidu', 'Taobao', 'Runoob', 'Google']\n"
     ]
    }
   ],
   "source": [
    "list1 = ['Google', 'Runoob', 'Taobao', 'Baidu']\n",
    "list1.reverse()\n",
    "print (\"列表反转后: \", list1)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 13.list.sort( key=None, reverse=False)\n",
    "对原列表进行排序"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "List :  ['Facebook', 'Google', 'Runoob', 'Taobao']\n"
     ]
    }
   ],
   "source": [
    "aList = ['Google', 'Runoob', 'Taobao', 'Facebook']\n",
    "\n",
    "aList.sort()\n",
    "print ( \"List : \", aList)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "降序输出: ['u', 'o', 'i', 'e', 'a']\n"
     ]
    }
   ],
   "source": [
    "# 列表\n",
    "vowels = ['e', 'a', 'u', 'o', 'i']\n",
    "\n",
    "# 降序\n",
    "vowels.sort(reverse=True)\n",
    "\n",
    "# 输出结果\n",
    "print ( '降序输出:', vowels )"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "排序列表： [(4, 1), (2, 2), (1, 3), (3, 4)]\n"
     ]
    }
   ],
   "source": [
    "# 获取列表的第二个元素\n",
    "def takeSecond(elem):\n",
    "    return elem[1]\n",
    "\n",
    "# 列表\n",
    "random = [(2, 2), (3, 4), (4, 1), (1, 3)]\n",
    "\n",
    "# 指定第二个元素排序\n",
    "random.sort(key=takeSecond)\n",
    "\n",
    "# 输出类别\n",
    "print ('排序列表：', random)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 14.list.clear()\n",
    "清空列表"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "列表清空后 :  []\n"
     ]
    }
   ],
   "source": [
    "list1 = ['Google', 'Runoob', 'Taobao', 'Baidu']\n",
    "list1.clear()\n",
    "print (\"列表清空后 : \", list1)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 15.list.copy()\n",
    "复制列表"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "list2 列表:  ['Google', 'Runoob', 'Taobao', 'Baidu']\n"
     ]
    }
   ],
   "source": [
    "list1 = ['Google', 'Runoob', 'Taobao', 'Baidu']\n",
    "list2 = list1.copy()\n",
    "print (\"list2 列表: \", list2)"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
